//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "vSAjoutLogiciel.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CEdit"
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
//      Constructeur
//---------------------------------------------------------------------------
__fastcall TAjoutLogiciel::TAjoutLogiciel(TComponent* Owner)
    : TForm(Owner)
{
    try {
        eNom->Clear();
        dmWebHelp = new TdmWebHelp((TComponent*)0);
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//      Destructeur
//---------------------------------------------------------------------------
__fastcall TAjoutLogiciel::~TAjoutLogiciel()
{
    try {
        if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
void __fastcall TAjoutLogiciel::bbOKClick(TObject *Sender)
{
    CHistorique * Hist = NULL;
    try {
        dmWebHelp->tLogiciels->Active = true;
        if( !Trim(eNom->Text).IsEmpty() ) {
            if( eNom->Text != "\\") {
                if(!VerifierExistance(eNom->Text)){
                    dmWebHelp->tLogiciels->Insert();
                    astLogiciels(LOG_NOM) = eNom->Text;
                    dmWebHelp->tLogiciels->Post();
                    dmWebHelp->trWebHelp->Commit();
                    ModalResult = mrOk;

                    Hist = new CHistorique();
                    Hist->AjouterOperation( Histo_AJOUT_LOG, eNom->Text );
                }else {
                    MessageDlg("Logiciel déjà existante", mtWarning, TMsgDlgButtons() << mbOK, 0);
                    ModalResult = mrNone;
                }
            }
        }else {
            MessageDlg("Veuillez remplir tous les champs", mtWarning, TMsgDlgButtons() << mbOK, 0);
            ModalResult = mrNone;
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(Hist) {delete Hist; Hist = NULL;}

}
//---------------------------------------------------------------------------
//      VERIFIER SI LOGICIEL ET COMMENTAIRE DEJA INSERER
//---------------------------------------------------------------------------
bool __fastcall TAjoutLogiciel::VerifierExistance(  const AnsiString &asLogiciel )
{
    bool bRetour = false;
    TZZMySqlQuery * MaRequete = NULL;
    try {

        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text = (AnsiString)"select log_nom from logiciels where log_nom="+QuotedStr(asLogiciel);
        MaRequete->Open();
        MaRequete->First();

        if( !asRequete(0).IsEmpty() )
            bRetour = true;

        MaRequete->Sql->Clear();
        MaRequete->Close();


    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    return bRetour;
}
//---------------------------------------------------------------------------
